<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Common\Model;

/**
 * Description of BookShelfModel
 *
 * @author gangguo
 */
class BookShelfModel extends XuecheModel{

    protected $pk = 'shelf_id';
    protected $tableName = 'book_shelf';
    protected $fields = array(
        'shelf_id','book_id','member_id','article_id','time','is_up'
    );


    public function Validate($data) {
        if($data['member_id'] > 0 && $data['book_id'] > 0 && $data['time']){
                return true;
        }
        return FALSE;
    }

    /**
     * 根据会员ID 获取我的书架
     * @param type $member_id
     * @return boolean
     */
    public function getShelfById($member_id){
        if($member_id > 0){
            $shelf = $this->where("`member_id` = '{$member_id}'")->select();
            if(is_array($shelf)){
                $Book = new BookModel();
                $Article = new BookArticleModel();
                foreach ($shelf as $key => $value) {
                    # code...
                     $info = $Book->getList("`book_id` = '{$value['book_id']}'");
                     $shelf[$key]['book_name'] = $info['lists'][0]['book_name'];
                     $shelf[$key]['status'] = $info['lists'][0]['status'];
                     $shelf[$key]['author_name'] = $info['lists'][0]['author_name'];
                     $shelf[$key]['article_name'] = $Article->getArticleName($value['article_id']);
                     $new = $Article->getNewArticle($value['book_id']);
                     $shelf[$key]['new_article_name'] = $new['article_name'];
                     $shelf[$key]['new_article_id'] = $new['article_id'];
                }
                return $shelf;
            }
        }
        return FALSE;
    }


    //同一个章节同一个会员只能添加一次
    public function checkReData($data){
        if($data['book_id'] > 0 && $data['member_id'] > 0){
            $where = "`member_id`='{$data['member_id']}' AND `book_id`='{$data['book_id']}'";
        }
        if($data['article_id'] > 0 && $where){
            $where .= " AND `article_id`='{$data['article_id']}'";
        }
        if($where && $this->where($where)->find()){
            return FALSE;
        }
        return true;
    }

    //获取会员的书籍收藏量
    public function getMyshelfNums($member_id){
        if($member_id > 0){
            return $this->where("`member_id`='{$member_id}'")->count($this->pk);
        }
        return 0;
    }





//put your code here
}
